package com.estronger.shopmanage.manager;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B11Designer;
import com.estronger.shopmanage.bean.B11Designer.Designer;
import com.estronger.shopmanage.ui.measure.MeasureActivity;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.view.AppDialog;
import com.estronger.shopmanage.view.ComPopWindow;
import com.estronger.shopmanage.view.RoundImageView;
import com.lidroid.xutils.http.RequestParams;
import com.soonfor.evaluate.tools.ImageUtils;
import com.tbruyelle.rxpermissions2.Permission;

import java.util.ArrayList;

public class PopManager implements OnClickListener {

    private Activity activity;
    /**
     * 扫描二维码popwind
     */
    private ComPopWindow serverPop;// 二维码识别时的PopWindow
    private ImageView imgfCover;
    public static final int FROM_PHOTO_CODE = 101;

    public PopManager(Activity activity, final ImageView imgfCover) {
        this.activity = activity;
        this.imgfCover = imgfCover;
        imgfCover.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                imgfCover.setVisibility(View.GONE);
            }
        });
    }

    // 初始化二维码识别时的PopWindow内容
    public void initServerPop() {
        serverPop = new ComPopWindow(activity, imgfCover);
        View view = LayoutInflater.from(activity).inflate(
                R.layout.popwindow_server, null);
        serverPop.setWidth(LayoutParams.MATCH_PARENT);
        serverPop.setHeight(LayoutParams.WRAP_CONTENT);
        serverPop.setBackgroundDrawable(new BitmapDrawable());
        serverPop.setFocusable(true);
        serverPop.setOutsideTouchable(true);
        serverPop.setContentView(view);
        RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);
        Button bt1 = (Button) view.findViewById(R.id.btn_outer_code);
        Button bt2 = (Button) view.findViewById(R.id.btn_local_code);
        parent.setOnClickListener(this);
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        serverPop.setAnimationStyle(R.style.dialogWindowAnim);
    }

    public void showServerPop(View parentView) {
        if (imgfCover != null) imgfCover.setVisibility(View.VISIBLE);
        serverPop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
    }

    public void hideServerPop() {
        if (imgfCover != null) imgfCover.setVisibility(View.GONE);
        serverPop.dismiss();
    }

    public boolean isServerPopShowing() {
        return serverPop.isShowing();
    }

    public ComPopWindow getServerPop() {
        return serverPop;
    }

    /**
     * 登录不成功提示
     */

    private ComPopWindow loginPop;// 登录时密码错误或用户名不存在的PopWindow

    // 初始化登录时密码错误或用户名不存在的PopWindow内容
    public void initLoginPop() {
        loginPop = new ComPopWindow(activity, imgfCover);
        View view = LayoutInflater.from(activity).inflate(
                R.layout.popwindow_login, null);
        loginPop.setBackgroundDrawable(new BitmapDrawable());
        loginPop.setFocusable(true);
        loginPop.setOutsideTouchable(true);
        loginPop.setContentView(view);
        loginPop.setWidth(LayoutParams.MATCH_PARENT);
        loginPop.setHeight(LayoutParams.MATCH_PARENT);
        title = (TextView) view.findViewById(R.id.login_pop_title);
        yes = (TextView) view.findViewById(R.id.login_pop_yes);
        forget_password = (TextView) view
                .findViewById(R.id.login_pop_forget_password);

        yes.setOnClickListener(this);
        // forget_password.setOnClickListener(this);
        loginPop.setAnimationStyle(R.style.pop_login);
    }

    public void showLoginPop(View parentView, boolean isWrongPass) {
        if (loginPop == null) {
            if (imgfCover != null) imgfCover.setVisibility(View.GONE);
            return;
        }if (isWrongPass) {
            if (imgfCover != null) imgfCover.setVisibility(View.VISIBLE);
            title.setText("密码输入错误，请重新输入密码!");
            forget_password.setVisibility(View.VISIBLE);
        } else {
            if (imgfCover != null) imgfCover.setVisibility(View.VISIBLE);
            title.setText("用户名不存在，请联系你的上级主管!");
            forget_password.setVisibility(View.GONE);
        }
        loginPop.showAtLocation(parentView, Gravity.CENTER, 0, 0);
    }

    public void hideLoginPop() {
        loginPop.dismiss();
    }

    public boolean isLoginPopShowing() {
        return loginPop.isShowing();
    }

    public ComPopWindow getLoginPop() {
        return loginPop;
    }


    /**
     * 联系客户
     */
    private PopupWindow contactPop;// 联系客户的PopWindow
    private PopupWindow transPop;// 转给其他同事的PopWindow

    private TextView title, yes, forget_password;


    public PopManager(Activity activity) {
        this.activity = activity;
    }


    // 初始化联系客户的PopWindow内容
    public void initContactPop() {
        contactPop = new PopupWindow(activity);
        View view = LayoutInflater.from(activity).inflate(
                R.layout.popwindow_contact, null);

        contactPop.setWidth(LayoutParams.MATCH_PARENT);
        contactPop.setHeight(LayoutParams.WRAP_CONTENT);
        contactPop.setBackgroundDrawable(new BitmapDrawable());
        contactPop.setFocusable(true);
        contactPop.setOutsideTouchable(true);
        contactPop.setContentView(view);

        Button bt2 = (Button) view.findViewById(R.id.contact_no);
        bt2.setOnClickListener(this);
        contactPop.setAnimationStyle(R.style.menushow);
    }

    public void showContactPop(View parentView) {
        contactPop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
    }

    public void hideContactPop() {
        contactPop.dismiss();
    }

    public boolean isContactPopShowing() {
        return contactPop.isShowing();
    }

    public PopupWindow getContactPop() {
        return contactPop;
    }

    private LinearLayout collegue_contoner;
    TextView trans_cts, collegue_name, trans_date;
    View collegue_pop, trans_contener, trans_back, trans_date_layout, do_trans;
    String measureid = "";

    // 初始化转给其他同事的PopWindow内容
    public void initTransPop(String measureid) {// ArrayList<Designers>
        // designers
        transPop = new PopupWindow(activity);
        View view = LayoutInflater.from(activity).inflate(
                R.layout.popwindow_trans, null);
        this.measureid = measureid;

        transPop.setWidth(LayoutParams.MATCH_PARENT);
        transPop.setHeight(LayoutParams.MATCH_PARENT);
        transPop.setBackgroundDrawable(new BitmapDrawable());
        transPop.setFocusable(true);
        transPop.setOutsideTouchable(true);
        transPop.setContentView(view);

        TextView bt2 = (TextView) view.findViewById(R.id.trans_cansel);
        trans_cts = (TextView) view.findViewById(R.id.trans_cts);
        collegue_name = (TextView) view.findViewById(R.id.collegue_name);
        trans_date = (TextView) view.findViewById(R.id.trans_date);
        View other = view.findViewById(R.id.collegue_name_layout);

        collegue_pop = view.findViewById(R.id.collegue_pop);
        collegue_contoner = (LinearLayout) collegue_pop
                .findViewById(R.id.collegue_contoner);
        trans_contener = view.findViewById(R.id.trans_contener);
        trans_back = view.findViewById(R.id.trans_back);
        do_trans = view.findViewById(R.id.do_trans);
        trans_date_layout = view.findViewById(R.id.trans_date_layout);

        trans_date_layout.setOnClickListener(this);
        trans_back.setOnClickListener(this);
        do_trans.setOnClickListener(this);
        other.setOnClickListener(this);
        bt2.setOnClickListener(this);
        transPop.setAnimationStyle(R.style.menushow);
    }

    // 初始化选择同事的PopWindow内容
    public void refleshTransPop(B11Designer designer) {
        collegue_name.setText(designer.data.plandesignername);
        collegue_name.setTag(designer.data.plandesignerid);
        trans_date.setText(designer.data.planmeasuredate);

        ArrayList<Designer> designers = designer.data.designers;
        if (designers != null && collegue_contoner.getChildCount() == 0) {
            for (int i = 0; i < designers.size(); i++) {
                RelativeLayout item = (RelativeLayout) View.inflate(activity,
                        R.layout.collegue_item, null);
                RoundImageView head = (RoundImageView) item
                        .findViewById(R.id.trans_photo);
                TextView name = (TextView) item.findViewById(R.id.trans_name);
                name.setText(designers.get(i).desc);
                MyApplication.imageLoader.displayImage(
                        NetManager.getDownloadURL() + "?filepath="
                                + designers.get(i).headpic, head);
                collegue_contoner.addView(item);

                item.setOnClickListener(this);
                item.setTag(designers.get(i).code);
            }
        }
    }

    public void showTransPop(View parentView, String name) {
        transPop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
        trans_cts.setText(name);
    }

    public void hideTransPop() {
        transPop.dismiss();
    }

    public boolean isTransPopShowing() {
        return transPop.isShowing();
    }

    public PopupWindow getTransPop() {
        return transPop;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // for server pop @+id/item_popupwindows_cancel
            case R.id.parent:
                serverPop.dismiss();
                break;
            case R.id.btn_outer_code:
                ImageUtils.sanBarCode(activity);
                break;
            case R.id.btn_local_code:
                PermissionUtils.getInstance().requestPromisssions(activity, new PermissionUtils.AcceptListener() {
                            @Override
                            public void grante() {
                                getImageUrlFromPhone(activity);
                                serverPop.dismiss();
                            }

                            @Override
                            public void denial(Permission permission) {
                            }

                            @Override
                            public void fail() {

                            }
                        }, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE);
                break;
            case R.id.login_pop_yes:
                loginPop.dismiss();
                break;
            // for contact pop
            case R.id.contact_no:
                contactPop.dismiss();
                break;
            // for trans pop
            case R.id.trans_cansel:
                transPop.dismiss();
                break;
            case R.id.collegue_name_layout:
                trans_contener.setVisibility(View.INVISIBLE);
                collegue_pop.setVisibility(View.VISIBLE);
                break;
            case R.id.trans_back:
                trans_contener.setVisibility(View.VISIBLE);
                collegue_pop.setVisibility(View.INVISIBLE);
                break;
            case R.id.trans_date_layout:
                // showPicker();
                AppDialog.createPicker(activity, trans_date).show();
                break;
            case R.id.collegue_item:
                collegue_name.setTag(v.getTag());
                TextView newName = (TextView) v.findViewById(R.id.trans_name);
                collegue_name.setText(newName.getText().toString());
                trans_contener.setVisibility(View.VISIBLE);
                collegue_pop.setVisibility(View.INVISIBLE);
                break;
            case R.id.do_trans:
                String planmeasuredate = trans_date.getText().toString();
                String plandesignerid = collegue_name.getTag().toString();

                RequestParams params = new RequestParams();
                params.addBodyParameter("sf_action", "editdesigner");
                params.addBodyParameter("usercode", MyApplication.getUserCode());
                params.addBodyParameter("measureid", measureid);
                params.addBodyParameter("plandesignerid", plandesignerid);
                params.addBodyParameter("planmeasuredate", planmeasuredate);
                ((MeasureActivity) (activity)).connect(params, NetManager.OTHER);
                transPop.dismiss();
                break;
            default:
                break;
        }

    }

    /**
     * 获取图片二维码是本地时的路径
     *
     * @param activity
     */
    public static void getImageUrlFromPhone(Activity activity) {
        Intent picture = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activity.startActivityForResult(picture, FROM_PHOTO_CODE);
    }
}
